package com.huazi.www.doctor.doctorg.servlet;

import com.alibaba.fastjson.JSON;
import com.huazi.www.doctor.doctorg.entity.QueueEntity;
import com.huazi.www.doctor.doctorg.service.impl.QueueServiceImpl;
import com.huazi.www.doctor.doctorg.util.Page;
import com.huazi.www.doctor.hu.entity.PatientInfo;
import com.huazi.www.doctor.hu.service.impl.DoctorServiceImpl;
import com.huazi.www.voice.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 候诊队列servlet
 */
public class QueueServlet extends HttpServlet {

    QueueServiceImpl service = new QueueServiceImpl();
    DoctorServiceImpl servicee = new DoctorServiceImpl();

    /**
     * 得到check.jsp的请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * 得到check.jsp的请求
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//req转类型
        String czf = req.getParameter("name");//resp转类型
        int doctorid = Integer.parseInt(req.getParameter("doctorid"));//把doctorid转为int类型
        if (czf == null) {
            czf = "";
        } //接受页数
        String current = req.getParameter("currentPage"); //接受页数
        if (current == null) {
            current = "1";
        }
        Integer currentPage = Integer.parseInt(current);//给接受页数（current）转为int
        String selecall = req.getParameter("selectall");//获取selectall
        if (selecall == null) {
            selecall = "";
        }

        if (selecall.equals("selectall")) {//操作符
            String name = req.getParameter("name");//获取姓名
            if (name == null) {//为空则改为字符串
                name = "";
            }
            Page<QueueEntity> page = service.page(doctorid, name, 1, currentPage);//给page<QueueEntity>传入值doctorid,name,1,currentPage
            req.setAttribute("page", page);//保存page
            req.setAttribute("state", "state");//保存state
            req.getRequestDispatcher("doctor/check.jsp").forward(req, resp);//转发check.jsp
            return;//放回
        } else if (czf.equals("update")) { //判断进入修改
            String id = req.getParameter("id");//得到用户的id修改状态
            Integer idd = Integer.parseInt(id);//给户的id转为int
            service.stateUpdate(idd);//给stateUpdate传入idd
            PrintWriter pw = resp.getWriter();//输出流
            String name = req.getParameter("upname");//获取upname
            String ts = "请" + name + "来问诊室问诊";//叫号
            Test.show(ts, "D://音频//out.mp3");//语音音频，音频路径D://音频//out.mp3
            String boo = JSON.toJSONString(id);//转JSON格式
            pw.print(boo);//要输出的东西
            pw.flush();//刷新
            pw.close();//关闭
            return;//放回
        }
        //得到文本框的值 查询
        //            检查
        com.huazi.www.doctor.hu.page.Page<PatientInfo> pat = servicee.selPat(Integer.parseInt("1"), doctorid);//获取病人分页对象
        req.setAttribute("pat", pat);//保存病人分页对象
        String name = req.getParameter("name");
        if (name == null) {
            name = "";
        }
        Page<QueueEntity> page = service.page(doctorid, name, 0, currentPage);//得到page的对象
        req.setAttribute("doctorid", doctorid);//返回医生的id
        req.setAttribute("doctorname", req.getParameter("doctorname"));//返回医生的name
        req.setAttribute("page", page);//返回分页集合
        req.setAttribute("name", name);//返回名字
        req.getRequestDispatcher("doctor/check.jsp").forward(req, resp);
    }
}
